
//创建子模块，一如子模块的依赖项目
var operateList = angular.module('operateOtherNew', ['tm.pagination', 'checklist-model', 'Common', 'operateS', 'loadModule', 'btnconqitk', 'personnelLabel', 'wangeditordrective', 'knowledgeSelector', 'operateNewAddEdit'])

.controller('operateOtherNew', ['$scope', '$rootScope', '$state', 'Conne_knowl', 'operatepathservice', 'Msg', 'MsgCon', 'sysUserInfo', function ($scope, $rootScope, $state, Conne_knowl, OS, Msg, MsgCon, sysUserInfo) {
    $scope.otherNewHide = false;
    $rootScope.secparstr = "index.honor.AnnouncementManagement"; //菜单选中
    // 初始化    
    $scope.currentTab = "index.honor.AnnouncementManagement";   //用于判断按扭权限使用
    $scope.powerLv = sysUserInfo.getuserinfo().powerLV; // 获取当前登录人的权限
    $scope.getAdminStyle = function () {    // 判断什么角色进来的
        if ($scope.powerLv == '2') {
            return { width: "200px" };
        }
    }
    // 配置知识选择器的参数
    $scope.knowChapter = {
        menu: ["file"], //["folder","file","courseware","structure","examination"],
        windowSize: "standard", //  窗口类型 standard:标准(默认)  mini :迷你
        ksfMore: false, //文件是否多选
        ksfList: [], // 文件列表
        ksfType: 0, // 文件类型 默认空  0 全部  1 文档 2 视频
        isShow: false// 显示知识选择器
        ,clear:true//回调时，清除选中的文件，
                        //此参数只有这里有，
                        //wang-edit在使用angular：ng-if时，无法绑定单击的回调事件，
                        //所以选择器只能使用ng-show，该属性无法触发选择器的初始事件，选择的文件会一直叠加
                        //所以需要传个参数手动清空一下
    };
    //配置富文本编辑器的按钮
    $scope.configbtnjishiben = ['undo', 'redo', '|', 'bold', 'underline', 'italic', 'forecolor', '|', 'fontsize', 'head', 'unorderlist', 'orderlist', 'alignleft', 'aligncenter', 'alignright', 'img','files'];
    $scope.init = function () {
        $scope.otherList = [];    //列表
        $scope.selectAll = 0; // 复选框选中的数量        
        $scope.params = { // 请求参数          
            pubSearch: "",    // 发布标题/发布人/内容
            pubCritical: "", // 是否紧急
            pageIndex: 1,
            pageSize: 10,
            powerLV: sysUserInfo.getuserinfo().powerLV,
            orgId: sysUserInfo.getuserinfo().organization_ID,
            userId: sysUserInfo.getuserinfo().user_ID
        };
        $scope.getList();   // 获取列表
    }
    // 复选框全选
    $scope.checkall = function () {
        if ($scope.selectAll == $scope.otherList.length) {
            angular.forEach($scope.otherList, function (data, index, array) {
                data.isflag = false;
            });
            $scope.selectAll = 0;
        } else {
            angular.forEach($scope.otherList, function (data, index, array) {
                data.isflag = true;
            });
            $scope.selectAll = $scope.otherList.length;
        }

    }
    // 选中单个数据
    $scope.checkSingle = function (item) {
        if (item.isflag) {
            $scope.selectAll--;
            item.isflag = false;
        } else {
            $scope.selectAll++;
            item.isflag = true;
        }
    }
    //搜索查询
    $scope.findlistbyEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            console.log('执行了回车');
            $scope.getList();
        }
    }
    // 获取公告列表
    $scope.getList = function () {
        $scope.operateLoading = true;   // 开启loading        
        // 测试end
        OS.findNotice($scope.params).then(function (response) {
            $scope.operateLoading = false;  //关闭loading
            if (response.errorcode == "0") {// 成功       
                $scope.otherList = response.datas;
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex
            } else {   // 获取失败 
                Msg.error("获取失败！");
            }
        }, function errorCallback() {
            $scope.operateLoading = false;  //关闭loading
            console.log("连接服务器失败！");
        });
    }

    // 删除公告管理
    $scope.delinfo = function () {
        $scope.otherIds = [];
        angular.forEach($scope.otherList, function (data, index, array) {
            if (data.isflag) {
                $scope.otherIds.push(data.noticeId);
            }
        });
        if ($scope.otherIds == null || $scope.otherIds == [] || $scope.otherIds.length == 0) {
            Msg.warning("请选择要删除的账号！");
            return;
        }
        //提示框
        MsgCon.delnoicon().then(function (flag) {   // 删除弹框
            if (flag) {
                console.log($scope.otherIds);
                // 发送请求
                OS.delNotice({ orgid: sysUserInfo.getuserinfo().organization_ID, userid: sysUserInfo.getuserinfo().user_ID, noticeId: $scope.otherIds }).then(function (respose) {
                    if (respose.errorcode == "0") {
                        Msg.success("操作成功！");
                        $scope.selectAll = 0; // 复选框选中的数量            
                        $scope.getList();
                    } else {
                        Msg.error("请求失败！");
                    }
                }, function errorCallback() {
                    console.log("连接服务器失败！");
                });
            }
        }, function (p) { console.log('error') });
    }
    /********************分配企业**********************/
    /* 
    * 弹框参数
    * otherNewHide 显示隐藏弹框
    */

    //sorgid:"",sorgname:"", 合并成一个对象 初始化可以不要
    $scope.otherNewItem = {
        isDisable: false,
        user_Img: sysUserInfo.getuserinfo().user_Img,
        userid: sysUserInfo.getuserinfo().user_ID,
        username: sysUserInfo.getuserinfo().user_Name,
        user_Account: sysUserInfo.getuserinfo().user_Account,
        pubLevel: sysUserInfo.getuserinfo().powerLV,
        pubTitle: "",
        pubType: "1", //公告类型  默认是发送给管理端，1，发送给学员  
        pubContent: "",
        pubCritical: ""
    };
    var otherItem = "";
    $scope.twoStageBtnLoad = false; // 控制按扭是否请求
    // 添加编辑方法
    $scope.distribution = function (item) {
        $scope.twoStageBtnLoad = false; // 发送请求
        $scope.otherItems = [];
        angular.forEach($scope.otherList, function (data, index, array) {
            if (data.isflag) {
                $scope.otherItems.push(data);
            }
        });
        // 点笔编辑
        if (item != null && item != "" && item != undefined) {  // 单个编辑的时候  重新初始化值
            $scope.otherItems = [];
            $scope.otherItems.push(item);
        }
        console.log($scope.otherItems.length);
        if ($scope.otherItems == null || $scope.otherItems == [] || $scope.otherItems.length == 0) {// 没有选择（新增）    
            $scope.otherNewItem = {
                isDisable: false,
                user_Img: sysUserInfo.getuserinfo().user_Img,
                userid: sysUserInfo.getuserinfo().user_ID,
                username: sysUserInfo.getuserinfo().user_Name,
                user_Account: sysUserInfo.getuserinfo().user_Account,
                pubLevel: sysUserInfo.getuserinfo().powerLV,
                pubTitle: "",
                pubContent: "",
                pubType: "1", //公告类型  默认是发送给管理端，1，发送给学员  
                pubCritical: false
            };
            $scope.otherNewItem.isDisable = false;  //标识 新增/编辑
            $scope.otherNewHide = true; // 显示编辑器            
        } else if ($scope.otherItems.length == 1) {// 只能选一个（编辑）
            otherItem = $scope.otherItems[0];
            $scope.otherNewItem = angular.copy($scope.otherItems[0]);   // 切断双向绑定
            $scope.otherNewItem.isDisable = true;  //标识 新增/编辑           
            console.log($scope.otherNewItem);
            // 显示编辑器            
            $scope.otherNewHide = true;
        } else if ($scope.otherItems.length > 1) {     //选择的太多了
            Msg.warning("只能选中一个账号！");
        }
    }
    // 点击的是取消方法
    $scope.cancelOtherNewItem = function () {
        $scope.otherNewHide = false;
    }
    //保存发布公告 通过指令调用的方法
    $scope.saveOtherNewItem = function (otherNewItem) {
        if (otherNewItem.pubTitle == null || otherNewItem.pubTitle == '' || otherNewItem.pubTitle == undefined) {
            Msg.warning("公告标题不能为空");
            return;
        }
        if (otherNewItem.pubContent == null || otherNewItem.pubContent == '' || otherNewItem.pubContent == undefined) {
            Msg.warning("公告内容不能为空");
            return;
        }
        $scope.twoStageBtnLoad = true; // 发送请求
        // 发送请求
        if (otherNewItem.noticeId == null || otherNewItem.noticeId == '' || otherNewItem.noticeId == undefined) {// 新增      
            var newitem = angular.copy(otherNewItem);
            newitem.pubContent = base64encode(encodeURI(newitem.pubContent));
            OS.addNotice(newitem).then(function (respose) {
                $scope.twoStageBtnLoad = false; // 发送请求
                if (respose.errorcode == "0") {         // 成功后需要
                    $scope.getList();
                    $scope.otherNewHide = false; // 隐藏编辑器             
                    Msg.success("操作成功！");
                } else {
                    Msg.error("请求失败！");
                }
            }, function errorCallback() {
                $scope.twoStageBtnLoad = false; // 发送请求
                console.log("连接服务器失败！");
            });
        } else { // 编辑            
            var newitem = angular.copy(otherNewItem);
            newitem.pubContent = base64encode(encodeURI(newitem.pubContent));
            OS.updateNotice(newitem).then(function (respose) {
                $scope.twoStageBtnLoad = false; // 发送请求
                if (respose.errorcode == "0") {         // 成功后需要
                    $scope.getList();
                    $scope.otherNewHide = false; // 隐藏编辑器       
                    Msg.success("操作成功！");
                } else {
                    Msg.error("请求失败！");
                }
            }, function errorCallback() {
                $scope.twoStageBtnLoad = false; // 发送请求
                console.log("连接服务器失败！");
            });
        }
        console.log(otherNewItem);
    }
    $scope.init();  // 加载调用初始化
    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            if ((parseInt($scope.params.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.params.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.params.pageIndex = $scope.paginationConf.currentPage;
                $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.getList();
            }
            $scope.selectAll = 0; // 复选框选中的数量     
        }
    };
} ]);

